<template>
  <el-form :model="loginForm"
           ref="adminLoginForm"
           :rules="adminRules"
           label-width="0px"
           label-position="left"
           @keydown.enter.native="login"
           class="login-container">
    <h3 class="login_title">管理员登录</h3>
    <el-form-item prop="account">
      <el-input type="text" v-model="loginForm.account" auto-complete="off" placeholder="管理员账号/医生工号"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码" show-password>
      </el-input>
    </el-form-item>
    <el-form-item >
      <div style="display: flex;align-items: center;justify-content: space-around">
        <span :style="[competence==='2'?optionsStyle:'']">医生</span>
        <el-switch
          v-model="competence"
          active-color="#646667FF"
          inactive-color="#505458"
          active-value="3"
          inactive-value="2">
        </el-switch>
        <span :style="[competence==='3'?optionsStyle:'']">管理员</span>
        <el-button type="primary" style="width: 50%;background: #505458;border: none" @click="login">登录
        </el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script>
import api from '@/apis/api';
import {valiDate} from "@/util/verify/valiDate";
import {adminLoginRules} from "@/util/verify/rules";


export default {
  name: "AdminLogin",
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      competence: "2",
      adminRules:adminLoginRules,
      optionsStyle:{
        fontWeight: "bold"
      }
    }
  },
  computed:{
    loginUrl(){
      return this.competence === "2" ? "/login/doctor": "/login/hospital"
    }
  },
  methods: {
    async login() {
      const _this = this;
      /* 执行表单验证 */
      const valid = await valiDate(_this,"adminLoginForm")
      if (!valid) return null
      const loginData = {
        'account': this.loginForm.account,
        'password': this.loginForm.password
      };
      /* 请求登录接口 */
      const loginResult = await api.adminLogin(this.loginUrl,loginData)
      if (loginResult.code === 200){
        await _this.$util.successMessage("登录成功")
        await _this.$store.dispatch("login",{token:loginResult.data,identity:parseInt(this.competence)})
        await _this.$Router.setAdminRouter(_this)
        _this.$router.replace({path: '/admin/'})
      }else{
        _this.$util.errorMessage(loginResult.msg);
      }
    }

  }
}
</script>

<style scoped>

</style>
